﻿Public Class frmMechanicViewer


    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
        frmMechanicAED.btnEdite.Text = "រក្សាទុក"
        frmMechanicAED.ShowDialog()
    End Sub

    Private Sub btnEdite_Click(sender As Object, e As EventArgs) Handles btnEdite.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)
        Dim frm = New frmMechanicAED
        frm.txtId.Text = row.Cells("Mechanic_id").Value
        frm.txtMechanicCode.Text = row.Cells("Mechanic_code").Value
        frm.txtMechanicName.Text = row.Cells("Mechanic_name").Value
        frm.cboSex.Text = row.Cells("sex").Value
        frm.txtTel.Text = row.Cells("phone").Value
        frm.txtEmail.Text = row.Cells("Email").Value
        frm.txtAdr.Text = row.Cells("isaddress").Value
        frm.cboProvince.Text = row.Cells("pro_Name").Value
        frm.btnEdite.Text = "កែប្រែ"
        If (frm.ShowDialog = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If
    End Sub

    Private Sub GroupBox3_Enter(sender As Object, e As EventArgs) Handles GroupBox3.Enter

    End Sub
    Public Sub LoadData()
        Cmd.Parameters("@S") = Me.txtSearch.Text
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select Mechanic_id,Mechanic_code,Mechanic_name,S.sex,phone,email,P.pro_Name,isaddress from tblMechanic M inner join tblProvice P ON P.Pro_id = M.provice_id inner join tblsex S ON S.sex_id = M.sex_id  WHERE UPPER(Mechanic_name+P.pro_Name) LIKE UPPER ('%'+@S+'%') AND M.Active = 1")
    End Sub
    Private Sub frmMechanicViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgv)
        LoadData()
    End Sub

    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("Mechanic_id").Value

        If MessageBox.Show("Are You Soure Want to Delete This Item?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Cmd.Parameters("@id") = id
        Cmd.ExecuteNonQuery("UPDATE tblMechanic SET Active = 0 where Mechanic_id = @id")
        LoadData()
    End Sub
End Class